import $ from '../utils';
import SignUp from '../controller/signUp';
import mongoDB from '../mongodb'

export default{
    login:async (req,res)=>{
        var signup = new SignUp();
        var data = req.body;
        let result = await signup.login(data);

        if(JSON.parse(result).status == '1'){
            req.session.regenerate((err)=>{
                if(!err){
                    req.session.token = $.baseFunc.createToken({"username":data.username});
                    res.send(result)
                }
            })
        }else{
            res.send(result)
        }
    },

    signup:async(req,res)=>{
         //console.log(req.session)
        //$.baseFunc.tokenPromise(req.session.token)
        let signup = new SignUp();
        let saltPass = await $.baseFunc.cryptPwd(req.body.pass);
        let userId = await $.baseFunc.cryptUserId(req.body.username);
        let data = {'username':req.body.username,'password':saltPass,'loginTime':$.base.currentTime(),
                    'registerTime':$.base.currentTime(),'user_id':userId,'user_image_url':''}
        let result = await signup.signUp(data);
        if(JSON.parse(result).status == '1'){
            req.session.regenerate((err)=>{
                if(!err){
                    req.session.token = $.baseFunc.createToken({"username":data.username});
                    res.send(result)
                }
            })
        }else{
            res.send(result)
        }


        //console.log($.config.MONGODB_CONN_STR)
    },

    loginOut:(req,res)=>{
         //console.log(req.session);
         req.session.destroy(function(err) {
            if(err){
                res.send(JSON.stringify({"status":'0',"msg":'退出登录失败',"dataset":[]}));
                return;
            }

            // req.session.loginUser = null;
            res.send(JSON.stringify({"status":'1',"msg":'退出登录成功',"dataset":[]}));
            //res.clearCookie($.config.sessionIdentityKey);
            //res.redirect('/');


        });
    },

    checkSession:(req,res)=>{
        res.send(JSON.stringify({"status":'1',"msg":'Session有效',"dataset":[]}));
    },

    queryUserInfo:async(req,res)=>{
        let result = await mongoDB.mongoFunc.findData({"user_id":req.body.user_id},'f_message_user');

        let resResult = {"username":result[0].username,"user_id":result[0].user_id,"user_image_url":result[0].user_image_url,
                         "user_registerTime":result[0].registerTime};

        let buffer = [resResult];

        res.send(JSON.stringify({"status":'1',"msg":'获取用户信息成功',"dataset":buffer}))
        // console.log(result)
    },

    changeUserInfo:async(req,res)=>{
        let queryData = {"user_id":req.body.user_id};
        let updateData = {"user_image_url":req.body.user_image_url};
        let result = await mongoDB.mongoFunc.updateData(queryData,updateData,'f_message_user');

        if(result){
            res.send(JSON.stringify({"status":'1',"msg":'更新成功',"dataset":[]}))
        }else{
            res.send(JSON.stringify({"status":'0',"msg":'更新失败',"dataset":[]}))
        }
    }
}

